﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShenLanCMS.Models;
using PagedList;

namespace ShenLanCMS.Web.Controllers
{
    public class TagController : Controller
    {
        private EntityContext db = new EntityContext();

        int pageSize = 20;

        public ViewResult Index()
        {
            return View(db.Tags.ToList());
        }

        //标签管理
        [IsAdmin]
        public ViewResult Admin(int? page)
        {
            var index = (page ?? 1) - 1;
            ViewData["actionName"] = "Admin";
            var tags = db.Tags.ToPagedList(index, pageSize);
            return View(tags);
        }

        [IsAdmin]
        public ActionResult Create()
        {
            return View();
        }

        [IsAdmin]
        [HttpPost]
        public ActionResult Create(Tag tag)
        {
            if (ModelState.IsValid)
            {
                tag.TagID = Guid.NewGuid();
                db.Tags.Add(tag);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(tag);
        }

        [IsAdmin]
        public ActionResult Delete(Guid id)
        {
            try
            {
                Tag tag = db.Tags.Find(id);
                db.Tags.Remove(tag);
                db.SaveChanges();
                return Json("删除成功");
            }
            catch { return Json("删除失败"); }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}